#include "pitches.h"

int inputPin1 = 2;
int inputPin2 = 3;
int inputPin3 = 4;
int inputPin4 = 5;
int inputPin5 = 6;
int inputPin6 = 7;
int inputPin7 = 8;
int inputPin8 = 9;
int buzzerPin = A4;
int val = 0;

int length = 1; //the number of notes
char notes[] = {'c' , 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int beats[] = {1};
int tempo = 300;

void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone*2) {
    digitalWrite(buzzerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(buzzerPin, LOW);
    delayMicroseconds(tone);
  }
}

void playNote(char note, int duration) {
  char names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
  int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
  
  //play the tone corresponding to the note name
  for(int i = 0; i < 8; i++) {
    if(names[i] == note) {
      playTone(tones[i], duration);
    }
  }
}

void setup() {
  Serial.begin(9600);
  pinMode(inputPin1, INPUT);
  pinMode(inputPin2, INPUT);
  pinMode(inputPin3, INPUT);
  pinMode(inputPin4, INPUT);
  pinMode(inputPin5, INPUT);
  pinMode(inputPin6, INPUT);
  pinMode(inputPin7, INPUT);
  pinMode(inputPin8, INPUT);
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  if (digitalRead(inputPin1) == 0) {
     playNote(notes[0], 300);
     Serial.println("Do");
//     Serial.print("1");
  } else if(digitalRead(inputPin2) == 0){
     playNote(notes[1], 300);
     Serial.println("RE");
     //     Serial.print("2");
  } else if(digitalRead(inputPin3) == 0){
     playNote(notes[2], 300);
     Serial.println("MI");
     //     Serial.print("3");
  } else if(digitalRead(inputPin4) == 0) {
     playNote(notes[3], 300);
     Serial.println("FA");
     //     Serial.print("4");
  } else if(digitalRead(inputPin5) == 0) {
     playNote(notes[4], 300);
     Serial.println("SO");
     //     Serial.print("5");
  } else if (digitalRead(inputPin6) == 0) {
     playNote(notes[5], 300);
     Serial.println("LA");
     //     Serial.print("6");
  } else if (digitalRead(inputPin7) == 0) {
     playNote(notes[6], 300);
     Serial.println("TI");
     //     Serial.print("7");
  } else if (digitalRead(inputPin8) == 0) {
     playNote(notes[7], 300);
     Serial.println("DO");
     //     Serial.print("8");
  } else {
    digitalWrite(buzzerPin, LOW);
  }
}
